package com.sbio.udiapp.ui;

import android.os.Bundle;

import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.sbio.udiapp.adapter.ComparePriceListViewAdapter;
import com.sbio.udiapp.common.bean.ExpressPriceResult;
import com.sbio.udiapp.databinding.ActivityComparePriceBinding;
import com.sbio.udiapp.net.api.ApiService;
import com.sbio.udiapp.net.bean.AjaxResult;
import com.sbio.udiapp.util.ApiServiceUtil;
import com.sbio.udiapp.util.DialogUtil;
import com.sbio.udiapp.util.LogUtil;
import com.sbio.udiapp.util.ToastUtil;
import com.sbio.udiapp.util.VibratorUtil;

import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@AndroidEntryPoint
public class ComparePriceActivity extends BaseBroadCastActivity {

    private ActivityComparePriceBinding binding;

    private ObservableList<ExpressPriceResult> datas = new ObservableArrayList<>();

    private ComparePriceListViewAdapter comparePriceListViewAdapter;

    @Inject
    ApiService apiService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityComparePriceBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.etWeight.requestFocus();

        comparePriceListViewAdapter = new ComparePriceListViewAdapter(this, datas);
        binding.priceTable.setAdapter(comparePriceListViewAdapter);
        binding.btnComparePrice.setOnClickListener(v -> {
            ApiServiceUtil.enqueue(() -> apiService.getPrice(binding.etWeight.getText().toString(), binding.etBillNo.getText().toString()), new Callback<AjaxResult>() {
                @Override
                public void onResponse(Call<AjaxResult> call, Response<AjaxResult> response) {
                    AjaxResult ajaxResult = response.body();
                    if (ajaxResult.isSuccess()) {
                        List<ExpressPriceResult> result = ajaxResult.getDataListByClz(ExpressPriceResult.class);
                        VibratorUtil.vibrator();
                        datas.clear();
                        datas.addAll(result);
                        comparePriceListViewAdapter.notifyDataSetChanged();
                        ToastUtil.show("比较成功，请查看价格");
                    } else {
                        ToastUtil.show(ajaxResult.getMsg());
                    }
                }

                @Override
                public void onFailure(Call<AjaxResult> call, Throwable t) {
                    DialogUtil.showCommonAlertDialog(t.getMessage());
                }
            });
        });

//        initSystemScanData();
    }

    @Override
    public void onBroadcastReceiver(String barcodeData) {
        LogUtil.file("接收到广播数据", barcodeData);
        //如果是出库申请开头的数据那么就是outStockApplyNos
        if (barcodeData.indexOf("CKSQ") == 0 || barcodeData.indexOf("FHTZ") == 0) {
            binding.etBillNo.setText(barcodeData);
        } else {
            //非法数据
            ToastUtil.showError("非法数据，请检查");
            return;
        }

//                saveOutStockApply();
    }


}